package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.ReleaseRentSeekingContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.RentSeekingDetail;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsReleaseRentSeeking;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/05/2019 14:39
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class ReleaseRentSeekingPresenter extends BasePresenter<ReleaseRentSeekingContract.Model, ReleaseRentSeekingContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public ReleaseRentSeekingPresenter(ReleaseRentSeekingContract.Model model, ReleaseRentSeekingContract.View rootView) {
        super(model, rootView);
    }

    /**
     * @param value
     * @param category
     */
    public void getApartmentRoom(String value, String category, List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans != null && !housePayWayBeans.isEmpty()) {
            mRootView.getApartmentRoomSuccess(housePayWayBeans);
            return;
        }//dictList4Category

        mModel.getApartmentRoom(Api.APP_DOMAIN_ZIDIAN + "/app/dictList", value, category).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getApartmentRoomSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * @param value
     * @param category
     */
    public void getApartmentDrawing(String value, String category, List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans != null && !housePayWayBeans.isEmpty()) {
            mRootView.getApartmentDrawingSuccess(housePayWayBeans);
            return;
        }//dictList4Category

        mModel.getApartmentDrawing(Api.APP_DOMAIN_ZIDIAN + "/app/dictList", value, category).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getApartmentDrawingSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * @param value
     * @param category
     */
    public void getApartmentToilet(String value, String category, List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans != null && !housePayWayBeans.isEmpty()) {
            mRootView.getApartmentToiletSuccess(housePayWayBeans);
            return;
        }//dictList4Category

        mModel.getApartmentToilet(Api.APP_DOMAIN_ZIDIAN + "/app/dictList", value, category).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getApartmentToiletSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 房屋要求
     *
     * @param value
     * @param category
     */
    public void getHouseRequirement(String value, String category, List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans != null && !housePayWayBeans.isEmpty()) {
            mRootView.getHouseRequirementSuccess(housePayWayBeans);
            return;
        }//dictList4Category

        mModel.getHouseRequirement(Api.APP_DOMAIN_ZIDIAN + "/app/dictList", value, category).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getHouseRequirementSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 获取区域信息
     *
     * @param value
     * @param housePayWayBeans
     */
    public void getHouseArea(String value, List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans != null && !housePayWayBeans.isEmpty()) {
            mRootView.getHouseArea(housePayWayBeans);
            return;
        }
        mModel.getAreaTree(Api.APP_DOMAIN_ZIDIAN + "/app/baseArea/getAreaTree", value, "").compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getHouseArea(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 获取类型
     *
     * @param housePayWayBeans
     */
    public void getReleaseType(List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans != null && !housePayWayBeans.isEmpty()) {
            mRootView.getReleaseTypeSuccess(housePayWayBeans);
            return;
        }
        mModel.getReleaseType(Api.SEEKING_TYPE).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getReleaseTypeSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 发布
     *
     * @param reqParamsReleaseRentSeeking
     */
    public void release(ReqParamsReleaseRentSeeking reqParamsReleaseRentSeeking) {
        mModel.release(AppUtil.entityToMap(reqParamsReleaseRentSeeking)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<RentSeekingDetail>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<RentSeekingDetail> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.releaseSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 修改
     *
     * @param seekingId
     * @param reqParamsReleaseRentSeeking
     */
    public void updateRentSeeking(String seekingId, ReqParamsReleaseRentSeeking reqParamsReleaseRentSeeking) {
        mModel.updateRentSeeking(seekingId, AppUtil.entityToMap(reqParamsReleaseRentSeeking)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<RentSeekingDetail>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<RentSeekingDetail> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.releaseSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 根据id获取详情
     *
     * @param seekingId
     */
    public void getDetail(String seekingId) {
        mModel.getDetail4Id(seekingId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<RentSeekingDetail>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<RentSeekingDetail> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getDetailSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
